package com.fuful.servlet;

import com.fuful.domain.Users;
import com.fuful.service.UsersService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 1.获得请求参数所对应的值
         * 2.调用业务逻辑
         * 3.根据逻辑结果选择相应视图(重定向、转发)
         */
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("username");
        String passWord = request.getParameter("upwd");
        UsersService usersService = new UsersService();
        Users myUser = usersService.login(userName,passWord);
        if(myUser != null){
            //启Session
            HttpSession session = request.getSession();
            session.setAttribute("user",myUser);
            response.sendRedirect("main.jsp");
        }else{
            response.sendRedirect("index.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
